//
//  Kratos_HttpResponse.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/7.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_HttpResponse.h"

@interface Kratos_HttpResponse()
//@property (nonatomic, copy, readwrite) NSData *Kra_rawData;
//@property (nonatomic, assign, readwrite) Kratos_HttpResponseStatus Kra_status;
//@property (nonatomic, copy) id Kra_content;
//@property (nonatomic, assign) NSInteger Kra_statueCode;
//@property (nonatomic, assign) NSInteger Kra_requestId;
//@property (nonatomic, copy, readwrite) NSURLRequest *Kra_request;

@end

@implementation Kratos_HttpResponse
- (nonnull instancetype)initWithRequestId:(nonnull NSNumber *)requestId
                                  Kratos_request:(nonnull NSURLRequest *)request
                             Kratos_responseData:(nullable NSData *)responseData
                                   Kratos_status:(Kratos_HttpResponseStatus)status{
    self = [super init];
    if (self)
    {
        self.Kra_requestId = [requestId unsignedIntegerValue];
        self.Kra_request = request;
        self.Kra_rawData = responseData;
        [self Kratos_inspectionResponse:nil];
    }
    return self;
}

- (nonnull instancetype)initWithRequestId:(nonnull NSNumber *)requestId
                                  Kratos_request:(nonnull NSURLRequest *)request
                             Kratos_responseData:(nullable NSData *)responseData
                                    Kratos_error:(nullable NSError *)error{
    self = [super init];
    if (self)
    {
        self.Kra_requestId = [requestId unsignedIntegerValue];
        self.Kra_request = request;
        self.Kra_rawData = responseData;
        [self Kratos_inspectionResponse:error];
    }
    return self;
}


///检查响应
- (void)Kratos_inspectionResponse:(NSError *)error
{
    if (error)
    {
        self.Kra_status = Kratos_HttpResponseStatusError;
        self.Kra_content = @"网络异常，请稍后再试";
        self.Kra_statueCode = error.code;
    }
    else
    {
        if (self.Kra_rawData.length > 0)
        {
            NSDictionary *dic = [self Kratos_jsonWithData:self.Kra_rawData];
            //1表示正常服务端 协定的
//            BOOL result = [dic[@"code"] integerValue] == 1;
            if (dic)
            {
                self.Kra_status = Kratos_HttpResponseStatusSuccess;
                self.Kra_content = [self Kratos_processCotnentValue:dic];
                NSString *code = dic[@"code"];
                if (code && [code isKindOfClass:[NSString class]]) {
                    self.Kra_statueCode = ((NSString*)code).integerValue;
                }
            }
            else
            {
                self.Kra_status = Kratos_HttpResponseStatusError;
                self.Kra_content = dic[@"msg"];
                NSString *code = dic[@"code"];
                if (code && [code isKindOfClass:[NSString class]]) {
                    self.Kra_statueCode = ((NSString*)code).integerValue;
                }
                if (![self.Kra_content isKindOfClass:[NSString class]]) {
                    self.Kra_content = @"未知错误";
                }
            }
        }
        else
        {
            self.Kra_statueCode = NSURLErrorUnknown;
            self.Kra_status = Kratos_HttpResponseStatusError;
            self.Kra_content = @"未知错误";
        }
    }
}
/**
 临时 返回数据处理
 */

- (id)Kratos_processCotnentValue:(id)content
{
    if ([content isKindOfClass:[NSDictionary class]])
    {
        NSMutableDictionary *contentDict = ((NSDictionary *)content).mutableCopy;
        [contentDict removeObjectForKey:@"result"];
        //        [contentDict removeObjectForKey:@"msg"];
        
        if ([contentDict[@"data"] isKindOfClass:[NSNull class]])
        {
            [contentDict removeObjectForKey:@"data"];
        }
        
        return contentDict.copy;
    }
    return content;
}

- (id)Kratos_jsonWithData:(NSData *)data { return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL]; }

@end
